﻿using System.Collections.Generic;
using WebApplication1.Models;
using System.Linq;
using WebApplication1.DAL;
using System;
using System.Data.Entity.Validation;

namespace WebApplication1.Controllers
{

    /// <summary>
    /// 模拟BLL层数据，这里没有连接DAL层
    /// </summary>
    public class EmployeeBusinessLayer
    {
        //定义一个Model对象List的对象
        public List<Employee> GetEmployees()
        {
            //实验五模拟没有DAL层
            //List<Employee> employees = new List<Employee>();
            //Employee emp = new Employee();

            //emp.FirstName = "johnson";
            //emp.LastName = " fernandes";
            //emp.Salary = 14000;
            //employees.Add(emp);

            //emp = new Employee();
            //emp.FirstName = "michael";
            //emp.LastName = "jackson";
            //emp.Salary = 16000;
            //employees.Add(emp);

            //emp = new Employee();
            //emp.FirstName = "robert";
            //emp.LastName = " pattinson";
            //emp.Salary = 20000;
            //employees.Add(emp);
            //return employees;



            SalesERPDAL salesDal = new SalesERPDAL();//新建一个DAL层对象  下追属性（找数据源）
            return salesDal.Employees.ToList();//通过调用其属性Employees返回对象列表，引用用LINQ，ToList()转换为List对象返回调用位置
        }

        internal UserDetails GetUserValidity()
        {
            throw new NotImplementedException();
        }

        //验证登陆
        public UserStatus GetUserValidity(UserDetails u)
        {
            if (u.UserName == "Admin" && u.Password == "Admin")
            {
                return UserStatus.AuthenticatedAdmin;
            }
            else if (u.UserName == "Sukesh" && u.Password == "Sukesh")
            {
                return UserStatus.AuthentucatedUser;
            }
            else
            {
                return UserStatus.NonAuthenticatedUser;
            }
        }

        public Employee SaveEmployee(Employee e)
        {
            SalesERPDAL salesDal = new SalesERPDAL();
            salesDal.Employees.Add(e);
            try
            {
                salesDal.SaveChanges();
            }
            catch (Exception)
            {

                throw new Exception("SQL插入语句执行异常");
            }
            return e;
        }
        

        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="employees"></param>
        public void UploadEmployees(List<Employee> employees)
        {
            SalesERPDAL salesDal = new SalesERPDAL();
            salesDal.Employees.AddRange(employees);
            try
            {
                salesDal.SaveChanges();
            }
            catch (DbEntityValidationException dbEx)
            {

                throw dbEx;
            }
        }
    }
}